/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Outils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.NotBoundException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import server.ServerHealth;
import server.Task;

/**
 *
 * @author smng2095
 */
public class LiaisonWS {
    private ServerHealth monitor;
    private String serveurWS,serveur;
     
   

   

    public LiaisonWS(String serveur, String serveurWS){
        this.serveur = serveur;
        this.serveurWS = serveurWS;
        
        try {
            URL url = new URL("http://"+serveurWS+":8080/Phase4-WS/webresources/ws/monitor/"+serveur+"/" );
            System.out.println(url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "text/xml");
            InputStream is = conn.getInputStream();
            
       // BufferedReader      in = new BufferedReader ( new InputStreamReader(is));
            JAXBContext jc = JAXBContext.newInstance(ServerHealth.class);
           
            
            monitor = (ServerHealth) jc.createUnmarshaller().unmarshal(is);
         
            
        } catch (MalformedURLException ex) {
            Logger.getLogger(LiaisonWS.class.getName()).log(Level.SEVERE, null, ex);
            monitor =null;
        } catch (IOException | JAXBException ex) {
            Logger.getLogger(LiaisonWS.class.getName()).log(Level.SEVERE, null, ex);
            monitor=null;
        }
        
        
    }
    public ServerHealth getMonitor(){
    return monitor;}
    
    
     public int getCpuLoad(){
    int r =0;
    r= monitor.getCpuLoad();
    return r;
    }
    
    public int getMemory(){
    int r =0;
    r = monitor.getMemory();
    return r ;
    }
    
    public Task getTask(int index){
	Task r = null;
	if(!(index <= monitor.getTasks().size()) | (index >= 0)) 
	    r = monitor.getTasks().get(index);
	return r ;
    }
    public List<Task> getArrayTask(){
	List<Task> r = null;
	r= monitor.getTasks();
	return r ;
    }
    
    
}
